<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" class="enhanced-dialog" width="50%">
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:folder" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">{{ dialogTitle }}</h4>
        <p class="dialog-subtitle">配置标签分组信息</p>
      </div>
    </div>

    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        v-loading="formLoading"
        class="enhanced-form"
      >
        <el-form-item label="分组名称" prop="name" class="form-item">
          <el-input v-model="formData.name" placeholder="请输入分组名称" class="enhanced-input">
            <template #prefix>
              <Icon icon="ep:edit" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="排序" prop="sort" class="form-item">
          <el-input-number
            :min="1"
            :max="100"
            :step="1"
            v-model="formData.sort"
            placeholder="请输入排序"
            style="width: 200px"
            class="enhanced-input-number"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark" class="form-item">
          <el-input
            v-model="formData.remark"
            placeholder="请输入备注"
            type="textarea"
            :rows="3"
            class="enhanced-textarea"
          >
            <template #prefix>
              <Icon icon="ep:document" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="是否共用" prop="isCommon" class="form-item switch-item">
          <div class="switch-container">
            <el-switch
              v-model="formData.isCommon"
              placeholder="是否共用"
              :active-value="1"
              :inactive-value="0"
              class="enhanced-switch"
            />
            <span class="switch-label">{{ formData.isCommon ? '共用' : '私有' }}</span>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <div class="footer-actions">
          <el-button @click="dialogVisible = false" class="action-button cancel-btn">
            <Icon icon="ep:close" class="btn-icon" />
            取 消
          </el-button>
          <el-button
            @click="submitForm"
            type="primary"
            :disabled="formLoading"
            class="action-button confirm-btn"
          >
            <Icon icon="ep:check" class="btn-icon" />
            确 定
          </el-button>
        </div>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as customerTagApi from '@/api/crm/customerTagConfig/index'

defineOptions({ name: 'CustomerTagConfigFrom' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  remark: undefined,
  isCommon: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '分组名称不能为空', trigger: 'blur' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await customerTagApi.getCustomerTag(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const props = defineProps({
  moduleId: {
    type: Number
  }
})

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  formData.value.moduleId = formData.value.isCommon == 1 ? 0 : props.moduleId
  try {
    if (formType.value === 'create') {
      await customerTagApi.createlabelSet(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await customerTagApi.updatelabelSet(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;
    box-shadow: $shadow-light;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
      max-height: 90vh;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }
}

/* 对话框头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }

  .header-icon {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: $border-radius-small;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      width: 36px;
      height: 36px;
      font-size: 16px;
    }
  }

  .header-content {
    flex: 1;
    margin-left: 12px;

    @media (max-width: 768px) {
      margin-left: 0;
    }
  }

  .dialog-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0 0 4px 0;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .dialog-subtitle {
    font-size: 13px;
    opacity: 0.9;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-form {
  .form-item {
    margin-bottom: 24px;

    @media (max-width: 768px) {
      margin-bottom: 20px;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 文本域样式 */
.enhanced-textarea {
  :deep(.el-textarea__inner) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;
    resize: vertical;
    min-height: 80px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &:focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 数字输入框样式 */
.enhanced-input-number {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }

  @media (max-width: 768px) {
    width: 100% !important;
  }
}

/* 开关项样式 */
.switch-item {
  .switch-container {
    display: flex;
    align-items: center;
    gap: 12px;

    @media (max-width: 768px) {
      justify-content: space-between;
      width: 100%;
    }
  }

  .enhanced-switch {
    :deep(.el-switch__core) {
      border-radius: 20px;
      transition: all 0.3s ease;

      &.is-checked {
        background-color: $primary-color;
        border-color: $primary-color;
      }
    }

    :deep(.el-switch__action) {
      transition: all 0.3s ease;
    }
  }

  .switch-label {
    font-size: 14px;
    color: $text-regular;
    font-weight: 500;
    padding: 4px 8px;
    background: $bg-color;
    border-radius: 4px;
    transition: all 0.3s ease;

    @media (max-width: 768px) {
      font-size: 13px;
    }
  }
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 20px 24px;
  background: $bg-color;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  .footer-actions {
    display: flex;
    gap: 12px;

    @media (max-width: 768px) {
      width: 100%;
      flex-direction: column;
      gap: 8px;
    }
  }
}

.action-button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: $border-radius-small;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 14px;
  }

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
    padding: 10px 16px;
  }

  &.confirm-btn {
    background: $primary-color;
    border-color: $primary-color;
    color: white;

    &:hover {
      background: #1d4ed8;
      border-color: #1d4ed8;
    }

    &:disabled {
      opacity: 0.6;
      cursor: not-allowed;
      transform: none;
    }
  }

  &.cancel-btn {
    background: $info-color;
    border-color: $info-color;
    color: white;

    &:hover {
      background: #82848a;
      border-color: #82848a;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #374151;
    border-top-color: #4b5563;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .dialog-subtitle {
    color: rgba(249, 250, 251, 0.9);
  }

  .switch-label {
    background: #4b5563;
    color: #d1d5db;
  }

  .enhanced-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-textarea {
    :deep(.el-textarea__inner) {
      background: #4b5563;
      border-color: #6b7280;
      color: #f9fafb;
    }
  }

  .enhanced-input-number {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-switch {
    :deep(.el-switch__core) {
      &.is-checked {
        background-color: #60a5fa;
        border-color: #60a5fa;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .form-container {
    padding: 12px;
  }

  .dialog-footer {
    padding: 12px 16px;
  }

  .action-button {
    padding: 8px 12px;
    font-size: 12px;
  }

  .enhanced-form .form-item {
    margin-bottom: 16px;
  }

  .switch-label {
    font-size: 12px;
  }
}
</style>
